from _collections import deque

from main.exceptions.exception import EmptyPCBGroupException, EmptyPCBListException
from main.model.pcb_management.pcb_group import PCBGroup

class PCBList(object):

    def __init__(self, priority, amount = 5):
        self.__max_x_group = amount
        self.__priority = priority
        self.__current_group = PCBGroup(self)
        self.__groups = deque()
        
    def __nonzero__(self) :
        return self.__is_not_empty() and 1 or 0 
    
    def __is_not_empty(self):
        return self.__current_group or self.__groups

    def add(self, pcb):
        if(self.__current_group.size() >= self.__max_x_group):
            self.store_current_group()
            self.__current_group = PCBGroup(self)
        self.__current_group.add(pcb)
        
    def store_current_group(self):
        self.__groups.append(self.__current_group)
    
    def next(self):
        try:
            return self.__current_group.next()
        except EmptyPCBGroupException:
            if(self.__groups):
                self.__current_group = self.__groups.popleft()
                return self.next();
            else:
                raise EmptyPCBListException()

    @property
    def priority(self):
        return self.__priority
    
    